<?php
header("Content-type: text/html; charset=utf-8");
require ROOT_PATH.'protected/include/ext/phpQuery/phpQuery.php';

class indexController extends baseController{
	static protected $visitpath='';//图片访问路径
	static protected $filepath ="";

	public function __construct()
	{
		parent::__construct();
		$this->visitpath=config('URL_HTTP_PROTOCOL').$_SERVER['HTTP_HOST']."/upload/collectibles/";
		$this->filepath=ROOT_PATH."upload/collectibles";
	}


	public function index(){
	    print_r(date("Y-m-d H:i:s",time()));
    }
	//http://auction.artron.net/SHJP-1377/PMH207671/PZ2030453
	public function getContentUrl(){
		$url=$_POST['url'];
		$page=$_POST['page'];
        if(!is_numeric($page)){
            exit;
        }
        for($num=0;$num<$page;$num++){
            $nowpage=$num+1;
            $this->createContent($this->getDetailUrl("$url-0-9-$nowpage/"));
        }
        return 'ok';
	}
	protected function createContent($fileUrl){
        if(is_array($fileUrl)){
            foreach($fileUrl as $val){
                $fileUrlList[]=$val;
            }
            if(is_array($fileUrlList)){
                foreach($fileUrlList as $key=>$item){
                    $infoDetail=$this->getInfoDetail($item);
                    $data['author']=$infoDetail['author'];
                    $data['content']=$infoDetail['auction_desc'];
                    $data['pictures']="collectibles/".$this->saveImage($infoDetail['img'],$this->filepath);
                    $auction_time=strtotime($infoDetail['auction_time']);
                    $data['title']=$infoDetail['title'];
                    $data['add_time']=$auction_time;

                    $company=model('companies')->find("apellation='".$infoDetail['company']."'","id","id DESC");
                    if(!$company){
                        $company_id=model('companies')->insert(array('apellation'=>$infoDetail['company']));
                    }else{
                        $company_id=$company['id'];
                    }

                    $expo=model('expo')->find("name='".$infoDetail['expo']."'","id","id DESC");

                    if(!$expo){
                        $expo_id=model('expo')->insert(array('name'=>$infoDetail['expo'],'company'=>$company_id));
                    }else{
                        $expo_id=$expo['id'];
                    }

                    $salesroom=model('salesroom')->find("room_name='".$infoDetail['concert']."'","id","id DESC");

                    if(!$salesroom){
                        $salesroom_id=model('salesroom')->insert(array(
                            'room_name'=>$infoDetail['concert'],
                            'expo_id'=>$expo_id,
                            'auction_starttime'=>$auction_time,
                            'company_id'=>$company_id));
                    }else{
                        $salesroom_id=$salesroom['id'];
                    }
                    $data['lot']=str_replace("*","",$infoDetail['lot']."");;
                    $data['dimension']=$infoDetail['size'];
                    if($infoDetail['currency']!='无'){
                        $data['currency']=$infoDetail['currency'];
                        $data['appraisal']=$infoDetail['price'];
                    }else{
                        $data['appraisal']=$infoDetail['currency'].$infoDetail['price'];
                    }

                    $data['salesroom_id']=$salesroom_id;

                    $data['expo_id']=$expo_id;
                    $data['companies_id']=$company_id;

                    $infoUNInsertID=model('collectibles')->find("expo_id=$expo_id AND companies_id=$salesroom_id AND lot='".$data['lot']."'");
                    echo $data['lot'];
                    if(!$infoUNInsertID){
                        $infoInsertID=model('collectibles')->insert($data);
                        if(!empty($infoInsertID)){//标签
                            $datt['category']=$infoDetail['category'];
                            $datt['sort']=$infoDetail['sort'];
                            $datt['times']=$infoDetail['times'];
                            $datt['log_id']=$infoInsertID;

                            if(is_array($datt)){
                                model("tags")->insert($datt);
                            }
                        }
                    }
                }
            }
        }
        return $infoUNInsertID;
    }
	protected function getDetailUrl($filename){
		$contentUrl=array();
		$baseUrl="http://auction.artron.net/";
		phpQuery::newDocumentFile($filename);
		$artlist = phpQuery::pq(".imgList");
		foreach($artlist as $li){
			$liData=pq($li)->find('li')->html();
			$pattern='/\"\/(.*)\" target=\"_blank\"/';
			preg_match_all($pattern,$liData,$arr,PREG_SET_ORDER);
			foreach($arr as $item){
				$contentUrl[]=$baseUrl.$item[1];
			}
			$arr=array();
		}
		$contentUrl=array_unique($contentUrl);
		return $contentUrl;
	}

	//获取文章大图
	protected function getInfoDetail($filename){
		phpQuery::newDocumentFile($filename);
		//get info content
		$arcInfo = phpQuery::pq(".worksInfo");
		foreach($arcInfo as $li){
			$detailInfoTable=pq($li)->find('table')->html();
			$detailInfoTable=preg_replace("/(\s|\&nbsp\;|　|\xc2\xa0)/", "", strip_tags($detailInfoTable));
			//Author
			$isAuthor=mb_strpos($detailInfoTable,'作者');
			$isSize=mb_strpos($detailInfoTable,'尺寸');
			$detail['author']=mb_substr($detailInfoTable,$isAuthor+6,$isSize-6);//作者
			$detailInfoTable=mb_substr($detailInfoTable,$isSize+6);
//Size
			$isType=mb_strpos($detailInfoTable,'作品分类');
			$detail['size']=mb_substr($detailInfoTable,0,$isType);//尺寸
			$detailInfoTable=mb_substr($detailInfoTable,$isType+12);
//Sort tags
			$isTimes=mb_strpos($detailInfoTable,'创作年代');
			$types=mb_substr($detailInfoTable,0,$isTimes);//作品分类
			$types=explode('&gt;',$types);
			if(count($types)==2){
				$detail['category']=$types[0];
				$detail['sort']=$types[1];
			}
			$detailInfoTable=mb_substr($detailInfoTable,$isTimes+12);
//Times
			$isValue=mb_strpos($detailInfoTable,'估价');
			$detail['times']=mb_substr($detailInfoTable,0,$isValue);//年代
			$detailInfoTable=mb_substr($detailInfoTable,$isValue+6);
//Concert
			$isExpo=mb_strpos($detailInfoTable,'专场');
			$envalue=mb_substr($detailInfoTable,0,$isExpo);
			$detail['currency']=substr($envalue,0,3);//估价
			$isDeal=mb_strpos($detailInfoTable,'成交价');

			$detail['price']=substr($envalue,3,$isDeal+24);//估价
			$detailInfoTable=mb_substr($detailInfoTable,$isExpo+6);
//Auction_Time
			$isAuctionTime=mb_strpos($detailInfoTable,'拍卖时间');
			$detail['concert']=mb_substr($detailInfoTable,0,$isAuctionTime);
			$detailInfoTable=mb_substr($detailInfoTable,$isAuctionTime+12);
//Company
			$isCompany=mb_strpos($detailInfoTable,'拍卖公司');
			$detail['auction_time']=mb_substr($detailInfoTable,0,$isCompany);
			$detailInfoTable=mb_substr($detailInfoTable,$isCompany+12);
//Auction_Show
			$isShow=mb_strpos($detailInfoTable,'拍卖会');
			$detail['company']=mb_substr($detailInfoTable,0,$isShow);
			$detailInfoTable=mb_substr($detailInfoTable,$isShow+9);

			$isExpo=mb_strpos($detailInfoTable,'拍卖会');
			$detail['expo']=mb_substr($detailInfoTable,0,$isExpo+9);

			$detailInfoTable=mb_substr($detailInfoTable,$isExpo+9);
			$isDesc=mb_strpos($detailInfoTable,'说明');
			if(!empty($isDesc)){
				$detail['auction_desc']=mb_substr($detailInfoTable,0,$isDesc);
				$detailInfoTable=mb_substr($detailInfoTable,$isDesc+6);
			}else{
				$detail['auction_desc']='';
			}
//auction_desc
			$isTWOCode=mb_strpos($detailInfoTable,'二维码');
			$detail['auction_desc']=$detail['auction_desc'].mb_substr($detailInfoTable,0,$isTWOCode);
		}
//get image
		$imgurl='';
		$artlist = phpQuery::pq(".imgCont");
		foreach($artlist as $li){
			$liData=pq($li)->find('div')->html();
			$pattern='/data-big=\"(.*)" /';
			preg_match_all($pattern,$liData,$arr,PREG_SET_ORDER);
			$imgurl=$arr[0][1];
			$imgurlarr=explode('"',$imgurl);
			$imgurl=$imgurlarr[0];
		}
		$detail['img']=$imgurl;

//get titile
		$arcTitle=phpQuery::pq(".baseTop");
		foreach($arcTitle as $li){
			$detailInfoTable=pq($li)->find('h1')->html();
			$startPis=strpos($detailInfoTable,' ');
			$detail['title']=substr($detailInfoTable,$startPis+1);
			$lots=explode(" ",$detailInfoTable);
			$detail['lot']=$lots[0];
		}
		return $detail;
	}

	protected function saveImage($img_url,$folder){
		$end=explode(".",$img_url);
		$end=".".$end[3];
		$pic_name=utime().rand(1111,9999).$end;
		//set_time_limit(24*60*60); //限制最大的执行时间
		$destination_folder=$folder?$folder.'/':''; //文件下载保存目录
		$newfname=$destination_folder.$pic_name;//文件PATH
		$file=fopen($img_url,'rb');

		if($file){
			$newf=fopen($newfname,'wb');
			if($newf){
				while(!feof($file)){
					fwrite($newf,fread($file,1024*8),1024*8);
				}
			}
			if($file){
				fclose($file);
			}
			if($newf){
				fclose($newf);
			}
			Image::thumb($newfname,$destination_folder.'64_'.$pic_name,'','640','640',6);
			return $pic_name;
		}
		return false;
	}

}